/**
 * 2017，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	ARptServiceImpl.java
 * Created：		2017年5月25日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.rpt;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;

import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.util.Assert;
import com.thor.palmmall.api.rpt.Report;
import com.thor.palmmall.api.rpt.ReportItem;
import com.thor.palmmall.api.rpt.passengerflow.DailyPassengerFlowReportParam;
import com.thor.palmmall.api.rpt.passengerflow.PassengerFlowRptService;
import com.thor.palmmall.api.rpt.sales.DailySalesReportParam;
import com.thor.palmmall.api.rpt.sales.MonthSalesReport;
import com.thor.palmmall.api.rpt.sales.SalesRptService;
import com.thor.palmmall.api.rpt.shop.RentalReportParam;
import com.thor.palmmall.api.rpt.shop.ShopService;
import com.thor.palmmall.api.rpt.vehicleflow.DailyVehicleFlowReportParam;
import com.thor.palmmall.api.rpt.vehicleflow.VehicleFlowRptService;

import edu.emory.mathcs.backport.java.util.Collections;

/**
 * @author wolv
 *
 */
public class ARptServiceImpl implements ARptService {

  Logger logger = LoggerFactory.getLogger(ARptServiceImpl.class);

  @Override
  public AHQBrief_v2 getHQBrief_v2(AHQBriefParam aParam) throws ThorServiceException {
    Assert.assertArgumentNotNull(aParam, "aParam");

    DailySalesReportParam salesAmountParam = new DailySalesReportParam();
    salesAmountParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
    salesAmountParam.setBeginDate(aParam.getStartDate());
    salesAmountParam.setEndDate(aParam.getEndDate());
    salesAmountParam.getStoreIds().clear();
    salesAmountParam.getStoreIds().addAll(aParam.getStoreIds());
    Report SalesAmountReport = salesRptService.getReport(salesAmountParam);

    // 销售报表
    DailySalesReportParam salesReportParam = new DailySalesReportParam();
    salesReportParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
    salesReportParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_STORE);
    salesReportParam.setBeginDate(aParam.getStartDate());
    salesReportParam.setEndDate(aParam.getEndDate());
    salesReportParam.getStoreIds().clear();
    salesReportParam.getStoreIds().addAll(aParam.getStoreIds());
    Report SalesReport = salesRptService.getReport(salesReportParam);

    // 客流报表
    DailyPassengerFlowReportParam passengerFlowParam = new DailyPassengerFlowReportParam();
    passengerFlowParam.setAggregationValueField(DailyPassengerFlowReportParam.VALUEFIELD_VALUE);
    passengerFlowParam.setDimensionField(DailyPassengerFlowReportParam.DIMENSIONFIELD_STORE);
    passengerFlowParam.getStoreIds().addAll(aParam.getStoreIds());
    passengerFlowParam.setBeginDate(aParam.getStartDate());
    passengerFlowParam.setEndDate(aParam.getEndDate());
    Report passengerFlow = passengerFlowRptService.getReport(passengerFlowParam);

    AHQBrief_v2 brief_v2 = new AHQBrief_v2();
    brief_v2.setSalesAmount(SalesAmountReport.getSingleValue());

    RsReport rsReport = new RsReport();
    fillReportWithSales(rsReport, SalesReport);
    fillReportWithPassaengerFlow(rsReport, passengerFlow);
    descReportItem(rsReport);

    brief_v2.setReport(rsReport);
    return brief_v2;
  }

  private void descReportItem(RsReport rsReport) {

    List<RsReportItem> items = rsReport.getItems();
    Collections.sort(items, new Comparator<RsReportItem>() {
      @Override
      public int compare(RsReportItem o1, RsReportItem o2) {
        return o2.getSaleAmount().compareTo(o1.getSaleAmount());
      }
    });
    rsReport.setItems(items);
  }

  private void fillReportWithPassaengerFlow(RsReport rsReport, Report passengerFlow) {
    if (passengerFlow == null) {
      return;
    }
    rsReport.setPassengerFlowSingleValue(passengerFlow.getSingleValue());
    rsReport.setPassengerFlowTotalValue(passengerFlow.getTotalValue());

    if (passengerFlow.getItems() == null) {
      return;
    }

    for (ReportItem rItem : passengerFlow.getItems()) {
      RsReportItem rsItem = new RsReportItem();
      rsItem.setDimension(rItem.getDimension());
      rsItem.setDimensionId(rItem.getDimensionId());
      rsItem.setPassengerFlowAmount(rItem.getValue());

      Iterator<RsReportItem> iter = rsReport.getItems().iterator();
      while (iter.hasNext()) {
        RsReportItem item = iter.next();
        if (item.getDimension().equals(rsItem.getDimension())) {
          rsItem.setSaleAmount(item.getSaleAmount());
          iter.remove();
        }
      }
      rsReport.getItems().add(rsItem);
    }
  }

  private void fillReportWithSales(RsReport rsReport, Report salesReport) {
    if (salesReport == null) {
      return;
    }
    rsReport.setSalesSingleValue(salesReport.getSingleValue());
    rsReport.setSalesTotalValue(salesReport.getTotalValue());

    if (salesReport.getItems() == null) {
      return;
    }

    for (ReportItem item : salesReport.getItems()) {
      RsReportItem rsItem = new RsReportItem();
      rsItem.setDimension(item.getDimension());
      rsItem.setDimensionId(item.getDimensionId());
      rsItem.setSaleAmount(item.getValue());
      rsReport.getItems().add(rsItem);
    }

  }

  @Override
  public AStoreBrief getStoreBrief(AStoreBriefParam aParam) throws ThorServiceException {
    Assert.assertArgumentNotNull(aParam, "aParam");
    Assert.assertArgumentNotNull(aParam.getDateRangeType(), "dateRangeType");
    Assert.assertArgumentNotNull(aParam.getStoreId(), "aParam.storeId");
    Assert.assertArgumentNotNull(aParam.getDate(), "aParam.date");

    AStoreBrief brief = new AStoreBrief();
    if (aParam.getDateRangeType() == ARptDateRangeType.month) {
      // 月销售额
      getReportByMonth(brief, aParam);

    } else {
      // 日销售额
      getReportByDay(brief, aParam);
    }
    return brief;
  }

  /**
   * @param brief
   * @throws ThorServiceException
   */
  private void getReportByDay(AStoreBrief brief, AStoreBriefParam aParam)
      throws ThorServiceException {
    StopWatch watch = new StopWatch("dailyReportWatch");
    watch.start("查询楼层面积");
    // 楼层面积
    Report floorArea = shopService.getFloorArea(aParam.getStoreId());
    watch.stop();

    watch.start("楼层铺位数");
    // 楼层铺位数
    Report floorShopCount = shopService.getFloorShopCount(aParam.getStoreId());
    watch.stop();
    watch.start("近N天销售");
    // 近N天销售
    DailySalesReportParam lastNDaysSalesAmountParam = new DailySalesReportParam();
    lastNDaysSalesAmountParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
    lastNDaysSalesAmountParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_SALESDATE);
    lastNDaysSalesAmountParam.setBeginDate(aParam.getSalesBeginDate());
    lastNDaysSalesAmountParam.setEndDate(aParam.getSalesEndDate());
    lastNDaysSalesAmountParam.setStoreId(aParam.getStoreId());
    Report lastNDaysSalesReport = salesRptService.getDailySalesReport(lastNDaysSalesAmountParam);
    brief.setLastNDaysSalesReport(lastNDaysSalesReport);
    watch.stop();
    watch.start("楼层销售");
    // 楼层销售
    DailySalesReportParam floorSalesAmountParam = new DailySalesReportParam();
    floorSalesAmountParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
    floorSalesAmountParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_FLOOR);
    floorSalesAmountParam.setDate(aParam.getDate());
    floorSalesAmountParam.setStoreId(aParam.getStoreId());
    Report floorSalesReport = salesRptService.getReport(floorSalesAmountParam);
    watch.stop();
    watch.start("客流");
    // 客流
    DailyPassengerFlowReportParam passengerFlowParam = new DailyPassengerFlowReportParam();
    passengerFlowParam.setAggregationValueField(DailyPassengerFlowReportParam.VALUEFIELD_VALUE);
    passengerFlowParam.setStoreId(aParam.getStoreId());
    passengerFlowParam.setDate(aParam.getDate());
    Report passengerReport = passengerFlowRptService.getReport(passengerFlowParam);
    watch.stop();
    
    watch.start("车流");
    // 车流
    DailyVehicleFlowReportParam vehicleFlowParam = new DailyVehicleFlowReportParam();
    vehicleFlowParam.setAggregationValueField(DailyVehicleFlowReportParam.VALUEFIELD_VALUE);
    vehicleFlowParam.setStoreId(aParam.getStoreId());
    vehicleFlowParam.setDate(aParam.getDate());
    Report report = vehicleFlowRptService.getReport(vehicleFlowParam);
    watch.stop();
    
    watch.start("楼层租金");
    // 楼层租金
    RentalReportParam floorRentParam = new RentalReportParam();
    floorRentParam.setAggregationValueField(RentalReportParam.VALUEFIELD_RENT);
    floorRentParam.setDimensionField(RentalReportParam.DIMENSIONFIELD_FLOOR);
    floorRentParam.setDate(aParam.getDate());
    floorSalesAmountParam.setStoreId(aParam.getStoreId());
    Report floorRentalReport = shopService.calcRentalReport(floorRentParam);
    watch.stop();
    
    watch.start("楼层收租");
    // 楼层收租
    RentalReportParam floorPaidParam = new RentalReportParam();
    floorPaidParam.setAggregationValueField(RentalReportParam.VALUEFIELD_PAID);
    floorPaidParam.setDimensionField(RentalReportParam.DIMENSIONFIELD_FLOOR);
    floorPaidParam.setDate(aParam.getDate());
    floorSalesAmountParam.setStoreId(aParam.getStoreId());
    Report floorPaidReport = shopService.calcRentalReport(floorPaidParam);
    watch.stop();
    logger.info("-----" + watch.prettyPrint());

    List<String> floors = new ArrayList<String>(floorArea.dimensionMap().keySet());
    Collections.sort(floors, new Comparator<String>() {
      @Override
      public int compare(String o1, String o2) {
        return o1.compareTo(o2);
      }
    });

    for (String floor : floors) {
      AStoreBriefFloorDetail detail = new AStoreBriefFloorDetail();
      brief.getFloorDetails().add(detail);

      detail.setFloor(floor);
      // 销售
      detail.setSalesAmount(floorSalesReport.dimensionMap().get(floor) != null
          ? floorSalesReport.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
      // 面积
      detail.setArea(floorArea.dimensionMap().get(floor) != null
          ? floorArea.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
      // 店铺数
      detail.setShopCount(floorShopCount.dimensionMap().get(floor) != null
          ? floorShopCount.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
      // 租金
      detail.setRent(floorRentalReport.dimensionMap().get(floor) != null
          ? floorRentalReport.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
      // 已收租金
      detail.setPaid(floorPaidReport.dimensionMap().get(floor) != null
          ? floorPaidReport.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
    }

    brief.setSalesAmount(floorSalesReport.getTotalValue());
    brief.setPassengerFlow(passengerReport.getSingleValue());
    brief.setVehicleFlow(report.getSingleValue());
    brief.setArea(floorArea.getTotalValue());
    brief.setRent(floorRentalReport.getTotalValue());
    brief.setPaid(floorPaidReport.getTotalValue());

  }

  private void getReportByMonth(AStoreBrief brief, AStoreBriefParam aParam)
      throws ThorServiceException {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(aParam.getDate());
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    final Date firstDay = calendar.getTime();
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
    calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
    final Date lastDay = calendar.getTime();

    StopWatch watch = new StopWatch("reportWatch");
    watch.start("查询楼层面积");
    // 楼层面积
    Report floorArea = shopService.getFloorArea(aParam.getStoreId());
    watch.stop();

    watch.start("楼层铺位数");
    // 楼层铺位数
    Report floorShopCount = shopService.getFloorShopCount(aParam.getStoreId());
    watch.stop();

    // 月销售
    watch.start("月销售");
    Report lastNMonthSalesAmountReport = getMonthSalesReport(aParam);
    brief.setLastNMonthsSalesReport(lastNMonthSalesAmountReport);
    watch.stop();

    // 月楼层销售
    watch.start("月楼层销售");
    Report floorSalesReport = getFloorSalesByMonth(aParam, firstDay, lastDay);
    watch.stop();

    // 客流
    watch.start("客流");
    DailyPassengerFlowReportParam passengerFlowParam = new DailyPassengerFlowReportParam();
    passengerFlowParam.setAggregationValueField(DailyPassengerFlowReportParam.VALUEFIELD_VALUE);
    passengerFlowParam.setBeginDate(firstDay);
    passengerFlowParam.setEndDate(lastDay);
    passengerFlowParam.setStoreId(aParam.getStoreId());
    Report passengerReport = passengerFlowRptService.getReport(passengerFlowParam);
    watch.stop();

    // 车流
    watch.start("车流");
    DailyVehicleFlowReportParam vehicleFlowParam = new DailyVehicleFlowReportParam();
    vehicleFlowParam.setAggregationValueField(DailyVehicleFlowReportParam.VALUEFIELD_VALUE);
    vehicleFlowParam.setStoreId(aParam.getStoreId());
    vehicleFlowParam.setBeginDate(firstDay);
    vehicleFlowParam.setEndDate(lastDay);
    Report report = vehicleFlowRptService.getReport(vehicleFlowParam);
    watch.stop();

    // 楼层租金
    watch.start("楼层租金");
    RentalReportParam floorRentParam = new RentalReportParam();
    floorRentParam.setAggregationValueField(RentalReportParam.VALUEFIELD_RENT);
    floorRentParam.setDimensionField(RentalReportParam.DIMENSIONFIELD_FLOOR);
    floorRentParam.setBeginDate(firstDay);
    floorRentParam.setEndDate(lastDay);
    floorRentParam.setStoreId(aParam.getStoreId());
    Report floorRentalReport = shopService.calcRentalReport(floorRentParam);
    watch.stop();

    // 楼层收租
    watch.start("楼层收租");
    RentalReportParam floorPaidParam = new RentalReportParam();
    floorPaidParam.setAggregationValueField(RentalReportParam.VALUEFIELD_PAID);
    floorPaidParam.setDimensionField(RentalReportParam.DIMENSIONFIELD_FLOOR);
    floorPaidParam.setBeginDate(firstDay);
    floorPaidParam.setEndDate(lastDay);
    floorPaidParam.setStoreId(aParam.getStoreId());
    Report floorPaidReport = shopService.calcRentalReport(floorPaidParam);
    watch.stop();
    logger.info("-----" + watch.prettyPrint());

    List<String> floors = new ArrayList<String>(floorArea.dimensionMap().keySet());
    Collections.sort(floors, new Comparator<String>() {
      @Override
      public int compare(String o1, String o2) {
        return o1.compareTo(o2);
      }
    });

    for (String floor : floors) {
      AStoreBriefFloorDetail detail = new AStoreBriefFloorDetail();
      brief.getFloorDetails().add(detail);

      detail.setFloor(floor);
      // 销售
      detail.setSalesAmount(floorSalesReport.dimensionMap().get(floor) != null
          ? floorSalesReport.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
      // 面积
      detail.setArea(floorArea.dimensionMap().get(floor) != null
          ? floorArea.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
      // 店铺数
      detail.setShopCount(floorShopCount.dimensionMap().get(floor) != null
          ? floorShopCount.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
      // 租金
      detail.setRent(floorRentalReport.dimensionMap().get(floor) != null
          ? floorRentalReport.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
      // 已收租金
      detail.setPaid(floorPaidReport.dimensionMap().get(floor) != null
          ? floorPaidReport.dimensionMap().get(floor).getValue() : BigDecimal.ZERO);
    }

    brief.setSalesAmount(floorSalesReport.getTotalValue());
    brief.setPassengerFlow(passengerReport.getSingleValue());
    brief.setVehicleFlow(report.getSingleValue());
    brief.setArea(floorArea.getTotalValue());
    brief.setRent(floorRentalReport.getTotalValue());
    brief.setPaid(floorPaidReport.getTotalValue());
  }

  /**
   * @param aParam
   * @return
   * @throws ThorServiceException
   */
  private Report getFloorSalesByMonth(AStoreBriefParam aParam, Date firstDay, Date lastDay)
      throws ThorServiceException {

    Calendar aCalendar = Calendar.getInstance();
    aCalendar.set(Calendar.MONTH, aCalendar.get(Calendar.MONTH) - 1);
    aCalendar.set(Calendar.DAY_OF_MONTH, aCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    aCalendar.set(Calendar.HOUR_OF_DAY, aCalendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    aCalendar.set(Calendar.MINUTE, aCalendar.getActualMaximum(Calendar.MINUTE));
    aCalendar.set(Calendar.SECOND, aCalendar.getActualMaximum(Calendar.SECOND));

    DailySalesReportParam floorSalesAmountParam = new DailySalesReportParam();
    floorSalesAmountParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
    floorSalesAmountParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_FLOOR);
    floorSalesAmountParam.setBeginDate(firstDay);
    floorSalesAmountParam.setEndDate(lastDay);
    floorSalesAmountParam.setStoreId(aParam.getStoreId());

    if (aParam.getDate().after(aCalendar.getTime())) {
      // 当前年月的数据直接查销售数据
      return salesRptService.getReport(floorSalesAmountParam);
    } else {
      // 指定年月的销售数据查历史报表
      Calendar anthorCalendar = Calendar.getInstance();
      anthorCalendar.setTime(aParam.getDate());
      anthorCalendar.set(Calendar.DAY_OF_MONTH, aCalendar.getActualMinimum(Calendar.DAY_OF_MONTH));
      anthorCalendar.set(Calendar.HOUR_OF_DAY, aCalendar.getActualMinimum(Calendar.HOUR_OF_DAY));
      anthorCalendar.set(Calendar.MINUTE, aCalendar.getActualMinimum(Calendar.MINUTE));
      anthorCalendar.set(Calendar.SECOND, aCalendar.getActualMinimum(Calendar.SECOND));

      floorSalesAmountParam.setDate(anthorCalendar.getTime());
      List<MonthSalesReport> floorSales = salesRptService
          .queryFloorSalesByMonth(floorSalesAmountParam);
      Report report = new Report();
      report.setItems(new ArrayList<ReportItem>());
      for (MonthSalesReport rpt : floorSales) {
        ReportItem item = new ReportItem();
        item.setDimension(rpt.getFloor());
        item.setDimensionId(rpt.getStoreId());
        item.setValue(rpt.getAmount());
        report.getItems().add(item);
      }
      return report;
    }
  }

  /**
   * @param AStoreBriefParam
   *          AStoreBriefParam
   * @return
   */
  private Report getMonthSalesReport(AStoreBriefParam aParam) throws ThorServiceException {
    final Date endDate = aParam.getSalesEndDate();

    DailySalesReportParam lastNMonthSalesAmountParam = new DailySalesReportParam();
    lastNMonthSalesAmountParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
    lastNMonthSalesAmountParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_SALESDATE);
    lastNMonthSalesAmountParam.setBeginDate(aParam.getSalesBeginDate());
    lastNMonthSalesAmountParam.setEndDate(aParam.getSalesEndDate());
    lastNMonthSalesAmountParam.setStoreId(aParam.getStoreId());

    // 取上个月的最后一天
    Calendar aCalendar = Calendar.getInstance();
    aCalendar.set(Calendar.MONTH, aCalendar.get(Calendar.MONTH) - 1);
    aCalendar.set(Calendar.DAY_OF_MONTH, aCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    aCalendar.set(Calendar.HOUR_OF_DAY, aCalendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    aCalendar.set(Calendar.MINUTE, aCalendar.getActualMaximum(Calendar.MINUTE));
    aCalendar.set(Calendar.SECOND, aCalendar.getActualMaximum(Calendar.SECOND));
    if (aParam.getSalesEndDate() != null) {
      // 查询月小于当前月前一月的最大时间，就默认查历史销售报表，否则直接查销售数据
      if (aParam.getSalesEndDate().before(aCalendar.getTime())) {
        return getMonthReportByHstReport(lastNMonthSalesAmountParam);
      } else {
        // 查询历史月数据报表
        lastNMonthSalesAmountParam.setEndDate(aCalendar.getTime());
        Report monthSalesReport = getMonthReportByHstReport(lastNMonthSalesAmountParam);

        // 当前月统计查销售数据生成报表
        lastNMonthSalesAmountParam.setBeginDate(aCalendar.getTime());
        lastNMonthSalesAmountParam.setEndDate(endDate);
        Report currentMonthSalesReport = salesRptService
            .getMonthSalesReport(lastNMonthSalesAmountParam);
        if (currentMonthSalesReport.getItems() != null) {
          monthSalesReport.getItems().addAll(currentMonthSalesReport.getItems());
        }
        return monthSalesReport;
      }
    }
    return salesRptService.getDailySalesReport(lastNMonthSalesAmountParam);
  }

  /**
   * 通过历史报表获取历史月销售数据
   * 
   * @param lastNMonthSalesAmountParam
   *          查询参数
   * @return
   * @throws ThorServiceException
   */
  private Report getMonthReportByHstReport(DailySalesReportParam lastNMonthSalesAmountParam)
      throws ThorServiceException {
    Report report = new Report();
    List<MonthSalesReport> mthRpt = salesRptService.queryMonthReport(lastNMonthSalesAmountParam);
    report.setItems(new ArrayList<ReportItem>());
    for (MonthSalesReport rpt : mthRpt) {
      SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
      ReportItem item = new ReportItem();
      item.setDimension(fmt.format(rpt.getDate()));
      item.setDimensionId(rpt.getStoreId());
      item.setValue(rpt.getAmount());
      report.getItems().add(item);
    }
    return report;
  }

  @Autowired
  private SalesRptService salesRptService;

  @Autowired
  private PassengerFlowRptService passengerFlowRptService;

  @Autowired
  private VehicleFlowRptService vehicleFlowRptService;

  @Autowired
  private ShopService shopService;

  @Override
  public AHQBrief getHQBrief(AHQBriefParam aParam) throws ThorServiceException {
    AHQBrief brief = new AHQBrief();
    DailySalesReportParam salesAmountParam = new DailySalesReportParam();
    salesAmountParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
    salesAmountParam.setBeginDate(aParam.getStartDate());
    salesAmountParam.setEndDate(aParam.getEndDate());
    salesAmountParam.getStoreIds().clear();
    salesAmountParam.getStoreIds().addAll(aParam.getStoreIds());
    Report SalesAmountReport = salesRptService.getReport(salesAmountParam);
    brief.setSalesAmount(SalesAmountReport.getSingleValue());
    // 销售报表
    DailySalesReportParam salesReportParam = new DailySalesReportParam();
    salesReportParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
    salesReportParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_STORE);
    salesReportParam.setBeginDate(aParam.getStartDate());
    salesReportParam.setEndDate(aParam.getEndDate());
    salesReportParam.getStoreIds().clear();
    salesReportParam.getStoreIds().addAll(aParam.getStoreIds());
    Report SalesReport = salesRptService.getReport(salesReportParam);
    brief.setStoreSalesReport(SalesReport);
    // 客流报表
    DailyPassengerFlowReportParam passengerFlowParam = new DailyPassengerFlowReportParam();
    passengerFlowParam.setAggregationValueField(DailyPassengerFlowReportParam.VALUEFIELD_VALUE);
    passengerFlowParam.setDimensionField(DailyPassengerFlowReportParam.DIMENSIONFIELD_STORE);
    passengerFlowParam.getStoreIds().addAll(aParam.getStoreIds());
    passengerFlowParam.setBeginDate(aParam.getStartDate());
    passengerFlowParam.setEndDate(aParam.getEndDate());
    Report passengerFlow = passengerFlowRptService.getReport(passengerFlowParam);
    brief.setStorePassengerFlowReport(passengerFlow);

    return brief;
  }

}
